package cn.edu.hqu.javaee.util;

import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import cn.edu.hqu.javaee.domain.Course;
import cn.edu.hqu.javaee.repository.CourseRepository;


@Component
public class CourseByldConverter implements Converter<String, Course> {
	
	private CourseRepository courseRepo;
	
	 @Autowired
	  public CourseByldConverter(CourseRepository courseRepo) {
	    this.courseRepo = courseRepo;
	  }
	  
	  @Override
	  public Course convert(String id) {
		  Optional<Course> optionalCourse = courseRepo.findById(id);
			return optionalCourse.isPresent() ?
				   optionalCourse.get() : null;
	  }
}
